package cn.ly.design.structure;

/**
 * 有一个Source类，拥有一个方法，待适配，目标接口时Target，
 * 通过Adapter类，将Source的功能扩展到Target里
 * @author liuyu
 *
 */
public class Adapter extends Source implements Target{

	@Override
	public void method2() {
		
	}
	
    public static void main(String[] args) {  
        Target target = new Adapter();  
        target.method1();  
        target.method2();  
    }  

}

class Source{
	public void method1(){
		System.out.println("this is source");
	}
}

interface Target{
	void method1();
	void method2();
}

/**
 * 不继承，持有Source类的实例，以达到解决兼容性
 * @author liuyu
 *
 */
class Adapter2 implements Target{
	private Source source;
	
	public Adapter2(Source source){
		this.source = source;
	}

	@Override
	public void method1() {
		this.source.method1();
	}

	@Override
	public void method2() {
		System.out.println("this is Adapter2 implments Target method2");
	}
	
}

